import sys
from typing import Union


def return_api_list(api_string):
    '''分隔api'''
    argv_list = []
    now_head = 0
    isa = 0  # in_string_ascii
    for api_char_number in range(len(api_string)):
        api_char = api_string[api_char_number]
        if api_char == "'":
            if isa == 0:
                isa = 1
            elif isa == 1:
                isa = 0
        elif api_char == '"':
            if isa == 0:
                isa = 2
            elif isa == 2:
                isa = 0
        elif api_char == ' ':
            if isa == 0:
                argv_list.append(api_string[now_head:api_char_number])
                now_head = api_char_number + 1
    return argv_list


def return_api_dict1(api_list):
    '''split of "-"'''
    isa = 0
    argv_dict = {}
    read_number = 0
    while read_number < len(api_list):
        read_text = api_list[read_number]
        if read_text[0] == '-':
            try:
                if api_list[read_number + 1][0] == '-':
                    argv_dict[api_list[read_number][1:]] = True
                    read_number += 1
                    continue
            except IndexError:
                argv_dict[api_list[read_number][1:]] = True
                read_number += 1
                continue
            argv_dict[api_list[read_number][1:]] = api_list[read_number + 1]
            read_number += 2
            continue
        argv_dict[api_list[read_number]] = None
        read_number += 1
    return argv_dict


class read_api:
    def __init__(self, api_string):
        self.api_string = ' '.join(api_string)
        self.MonitorApiList = []

    def Add_Monitor_api(self, apiName: Union[str, list], DefaultValue=None):
        self.MonitorApiList.append([apiName, DefaultValue])

    def return_dict(self, auto_plus_space=True):
        api_String = self.api_string
        if auto_plus_space:
            api_String = self.api_string + ' '
        api_list = return_api_list(api_String)
        api_dict = return_api_dict1(api_list)
        return api_dict

    def start(self):
        rd = {}
        argdict = self.return_dict()
        for _ in self.MonitorApiList:
            if type(_[0]) == type([]):
                args = _[0]
                for arg in args:
                    try:
                        value = argdict[arg]
                    except:
                        pass

            try:
                rd[_[0]] = dict[_[0]]
            except:
                rd[_[0]] = _[1]
        return rd


def start():
    argv = ' '.join(sys.argv)
    return read_api(argv).return_dict()
